package org.xserver.component.core.interfaces;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.stereotype.Component;

/**
 * Prefix project name to original interface. The reason is we use nginx proxy
 * all projects, and the entry is same, so we should use nginx's location to
 * redirect to every project server.
 * 
 * @author idoldefine
 * @since 2016/11/28
 *
 */
@Component
public class BossInterfaceResolverManager extends InterfaceResolverManager {

	/**
	 * wrap original interfaces with project prefix.
	 */
	@Override
	protected Map<String, InterfaceMeta> getInterfaceContext() {
		Map<String, InterfaceMeta> interfaceContextSource = super
				.getInterfaceContext();
		Map<String, InterfaceMeta> interfaceContext = new HashMap<String, InterfaceMeta>(
				interfaceContextSource.size());

		for (Entry<String, InterfaceMeta> entry : interfaceContextSource
				.entrySet()) {
			InterfaceMeta interfaceMeta = entry.getValue();
			String name = interfaceMeta.getClazz().getName();
			String project = name.split("\\.")[2];

			String key = InterfaceContext.DEFAULT_INTERFACE_SPLIT + project
					+ entry.getKey();

			interfaceContext.put(key, interfaceMeta);
		}

		return interfaceContext;
	}
}
